home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / dataex / datasamp.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-07  |  11.2 KB  |  383 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Data Control Example"
  4.    ClientHeight    =   4155
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1575
  7.    ClientWidth     =   7020
  8.    Height          =   4560
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4155
  12.    ScaleWidth      =   7020
  13.    Top             =   1230
  14.    Width           =   7140
  15.    Begin TextBox Text1 
  16.       DataField       =   "bitfield"
  17.       DataSource      =   "data1"
  18.       Height          =   285
  19.       Left            =   2400
  20.       TabIndex        =   18
  21.       Top             =   360
  22.       Width           =   1215
  23.    End
  24.    Begin CommandButton cmdGrid 
  25.       Caption         =   "Show Grid"
  26.       Height          =   375
  27.       Left            =   4200
  28.       TabIndex        =   17
  29.       Top             =   3720
  30.       Width           =   1095
  31.    End
  32.    Begin CommandButton cmdEnd 
  33.       Caption         =   "End"
  34.       Height          =   375
  35.       Left            =   5640
  36.       TabIndex        =   16
  37.       Top             =   2520
  38.       Width           =   1095
  39.    End
  40.    Begin TextBox txtID 
  41.       DataField       =   "scount"
  42.       DataSource      =   "Data1"
  43.       Enabled         =   0   'False
  44.       Height          =   285
  45.       Left            =   600
  46.       TabIndex        =   0
  47.       Top             =   360
  48.       Width           =   1335
  49.    End
  50.    Begin CommandButton cmdDelete 
  51.       Caption         =   "Delete"
  52.       Height          =   375
  53.       Left            =   4200
  54.       TabIndex        =   12
  55.       Top             =   1320
  56.       Width           =   1095
  57.    End
  58.    Begin CommandButton cmdCancel 
  59.       Caption         =   "Cancel"
  60.       Height          =   375
  61.       Left            =   4200
  62.       TabIndex        =   11
  63.       Top             =   3120
  64.       Width           =   1095
  65.    End
  66.    Begin CommandButton cmdSave 
  67.       Caption         =   "Save"
  68.       Height          =   375
  69.       Left            =   4200
  70.       TabIndex        =   10
  71.       Top             =   2520
  72.       Width           =   1095
  73.    End
  74.    Begin CommandButton cmdAdd 
  75.       Caption         =   "Add"
  76.       Height          =   375
  77.       Left            =   4200
  78.       TabIndex        =   9
  79.       Top             =   1920
  80.       Width           =   1095
  81.    End
  82.    Begin CommandButton cmdUpdate 
  83.       Caption         =   "Update"
  84.       Enabled         =   0   'False
  85.       Height          =   375
  86.       Left            =   4200
  87.       TabIndex        =   8
  88.       Top             =   720
  89.       Width           =   1100
  90.    End
  91.    Begin CommandButton cmdFind 
  92.       Caption         =   "Find"
  93.       Height          =   375
  94.       Left            =   4200
  95.       TabIndex        =   7
  96.       Top             =   120
  97.       Width           =   1100
  98.    End
  99.    Begin CommandButton cmdLast 
  100.       Caption         =   "Last"
  101.       Height          =   375
  102.       Left            =   5640
  103.       TabIndex        =   6
  104.       Top             =   1920
  105.       Width           =   1100
  106.    End
  107.    Begin CommandButton cmdFirst 
  108.       Caption         =   "First"
  109.       Height          =   375
  110.       Left            =   5640
  111.       TabIndex        =   5
  112.       Top             =   1320
  113.       Width           =   1100
  114.    End
  115.    Begin CommandButton cmdPrior 
  116.       Caption         =   "Prior"
  117.       Height          =   375
  118.       Left            =   5640
  119.       TabIndex        =   4
  120.       Top             =   720
  121.       Width           =   1100
  122.    End
  123.    Begin CommandButton cmdNext 
  124.       Caption         =   "Next"
  125.       Height          =   375
  126.       Left            =   5640
  127.       TabIndex        =   3
  128.       Top             =   120
  129.       Width           =   1100
  130.    End
  131.    Begin TextBox txtFirstName 
  132.       DataField       =   "class"
  133.       DataSource      =   "Data1"
  134.       Height          =   375
  135.       Left            =   600
  136.       TabIndex        =   2
  137.       Top             =   1920
  138.       Width           =   3135
  139.    End
  140.    Begin TextBox txtLastName 
  141.       DataField       =   "name"
  142.       DataSource      =   "Data1"
  143.       Height          =   375
  144.       Left            =   600
  145.       TabIndex        =   1
  146.       Top             =   1080
  147.       Width           =   3135
  148.    End
  149.    Begin Data Data1 
  150.       Caption         =   "Data1"
  151.       Connect         =   ""
  152.       DatabaseName    =   "asample.MDB"
  153.       Exclusive       =   0   'False
  154.       Height          =   270
  155.       Left            =   600
  156.       Options         =   0
  157.       ReadOnly        =   0   'False
  158.       RecordSource    =   "student"
  159.       Top             =   2640
  160.       Width           =   3135
  161.    End
  162.    Begin Label Label1 
  163.       Caption         =   "BitField"
  164.       Height          =   255
  165.       Left            =   2400
  166.       TabIndex        =   19
  167.       Top             =   120
  168.       Width           =   975
  169.    End
  170.    Begin Label lblID 
  171.       Caption         =   "Scount"
  172.       Height          =   255
  173.       Left            =   600
  174.       TabIndex        =   15
  175.       Top             =   120
  176.       Width           =   1095
  177.    End
  178.    Begin Label lblFirstName 
  179.       Caption         =   "Class"
  180.       Height          =   255
  181.       Left            =   600
  182.       TabIndex        =   14
  183.       Top             =   1680
  184.       Width           =   1335
  185.    End
  186.    Begin Label lblLastName 
  187.       Caption         =   "Name"
  188.       Height          =   255
  189.       Left            =   600
  190.       TabIndex        =   13
  191.       Top             =   840
  192.       Width           =   975
  193.    End
  194. Option Explicit
  195. Const SIZE = 11
  196. Dim sQuote As String
  197. Dim src As String
  198. Dim criteria As String
  199. Dim currentrecord As String
  200. Const ERROR_MSG = "You have entered an Employee ID that already exists. Modify Employee ID before proceeding."
  201. Const KEY_DELETE = &H2E
  202. Sub cmdAdd_Click ()
  203.     currentrecord = data1.Recordset.Bookmark
  204.     data1.Recordset.AddNew
  205.     'Show Save and Cancel - Disable Delete
  206.     cmdSave.Visible = True
  207.     cmdCancel.Visible = True
  208.     cmdDelete.Enabled = False
  209.     txtID.Enabled = True
  210.     txtID.SetFocus
  211. End Sub
  212. Sub cmdCancel_Click ()
  213.     'Restore position to where you were
  214.     'when you started Adding records
  215.      data1.Recordset.Bookmark = currentrecord
  216.     'Reset buttons to show leaving Add mode
  217.     ReSetButtons
  218. End Sub
  219. Sub cmdDelete_Click ()
  220.     On Error Resume Next
  221.     data1.Recordset.Delete
  222.     data1.Recordset.MoveNext
  223.     'If the last record was just deleted, move to the
  224.     'new last record
  225.     If data1.Recordset.EOF Then
  226.         data1.Recordset.MoveLast
  227.     End If
  228. End Sub
  229. Sub cmdEnd_Click ()
  230.     Unload form1
  231. End Sub
  232. Sub cmdFind_Click ()
  233.     sQuote = Chr$(34)
  234.     'If Save is visible - you were in the middle of adding
  235.     'Hide Save/Cancel to show ADD was cancelled.
  236.     cmdSave.Visible = False
  237.     cmdCancel.Visible = False
  238.     'Save the current position.
  239.     currentrecord = data1.Recordset.Bookmark
  240.     src = InputBox$("Enter last name of employee to find", "Find Box")
  241.     If src = "" Then Exit Sub
  242.     criteria = "[Last Name] = " & sQuote & src & sQuote
  243.     data1.Recordset.FindFirst criteria
  244.     If data1.Recordset.NoMatch Then
  245.         MsgBox "No Record Found!", , "What were you thinking?"
  246.         data1.Recordset.Bookmark = currentrecord
  247.     Else
  248.         cmdNext.Enabled = True
  249.         cmdPrior.Enabled = True
  250.     End If
  251. End Sub
  252. Sub cmdFirst_Click ()
  253.     data1.Recordset.MoveFirst
  254.     cmdNext.Enabled = True
  255.     cmdPrior.Enabled = False
  256. End Sub
  257. Sub cmdGrid_Click ()
  258.     form1.Hide
  259.     LoadFrame
  260.     form2.Show
  261. End Sub
  262. Sub cmdLast_Click ()
  263.     data1.Recordset.MoveLast
  264.     cmdPrior.Enabled = True
  265.     cmdNext.Enabled = False
  266. End Sub
  267. Sub cmdNext_Click ()
  268.     data1.Recordset.MoveNext
  269.     cmdPrior.Enabled = True
  270.     If data1.Recordset.EOF Then
  271.         data1.Recordset.MoveLast
  272.         cmdNext.Enabled = False
  273.     End If
  274. End Sub
  275. Sub cmdPrior_Click ()
  276.     data1.Recordset.MovePrevious
  277.     cmdNext.Enabled = True
  278.     If data1.Recordset.BOF Then
  279.         data1.Recordset.MoveFirst
  280.         cmdPrior.Enabled = False
  281.     End If
  282. End Sub
  283. Sub cmdSave_Click ()
  284.     On Error GoTo SaveError
  285.     data1.UpdateRecord
  286.     cmdSave.Visible = False
  287.     cmdCancel.Visible = False
  288.     'Reset buttons to show leaving Add mode
  289.     ReSetButtons
  290.     'Disable the key field
  291.     txtID.Enabled = False
  292. TerminateSave:
  293.     Exit Sub
  294. SaveError:
  295.     If Err = 3022 Then
  296.         MsgBox ERROR_MSG
  297.         txtID.SelStart = 0
  298.         txtID.SelLength = Len(txtID.Text)
  299.         txtID.SetFocus
  300.     Else
  301.         MsgBox Error$
  302.     End If
  303.     Resume TerminateSave
  304. End Sub
  305. Sub cmdUpdate_Click ()
  306.     'Invoke Data1.UpdateRecord rather
  307.     'than Data1.RecordSet.Update.
  308.     'Data1.UpdateRecord saves a record
  309.     'but DOES NOT invoke Validation Event.
  310.     'We don't want to prompt user to save
  311.     'if he or she explicitly chose Update.
  312.     data1.UpdateRecord
  313.     cmdUpdate.Enabled = False
  314. End Sub
  315. Sub Data1_Error (DataErr As Integer, response As Integer)
  316.     Const CONTINUE = 0
  317.     If DataErr = 3022 Then
  318.         MsgBox ERROR_MSG
  319.         txtID.SelStart = 0
  320.         txtID.SelLength = Len(txtID.Text)
  321.         txtID.SetFocus
  322.         response = CONTINUE
  323.     End If
  324. End Sub
  325. Sub Data1_Reposition ()
  326.     cmdUpdate.Enabled = False
  327. End Sub
  328. Sub Data1_Validate (Action As Integer, Save As Integer)
  329.     Const IDNO = 7
  330.     If Save = True Then
  331.         'We will prompt the user to save record
  332.         'for all actions EXCEPT ADD.
  333.         'If the user selected ADD then ADD again,
  334.         'we do not want to issue prompt. (Ation 5 is Add)
  335.         'However, if Update button is visible, that
  336.         'means the user user modified a record
  337.         'but has not saved it yet.
  338.         'In this case, we should issue prompt.
  339.         If Action <> 5 Or cmdUpdate.Enabled = True Then
  340.             If MsgBox("save?", 4) = IDNO Then
  341.               Save = False
  342.               data1.UpdateControls
  343.           End If
  344.         End If
  345.     End If
  346.     If Action <> 5 Then
  347.         'Reset Buttons to show leaving Add mode
  348.          ReSetButtons
  349.     End If
  350. End Sub
  351. Sub Form_Load ()
  352.     'make Save and Cancel invisible
  353.     cmdSave.Visible = False
  354.     cmdCancel.Visible = False
  355. End Sub
  356. Sub ReSetButtons ()
  357.     'ReEnableButtons is called whenever you Cancel
  358.     'or save a record. It simply hides Save and Cancel
  359.     'to show you are no longer in Add mode
  360.     'and reenables Delete. (Delete is only valid
  361.     'if you are not in Add mode.)
  362.     'This routine is invoked from cmdSave, cmdCancel,
  363.     'cmdFind and the Validation Event of data control
  364.     cmdSave.Visible = False
  365.     cmdCancel.Visible = False
  366.     cmdDelete.Enabled = True
  367. End Sub
  368. Sub txtFirstName_KeyPress (KeyAscii As Integer)
  369.     If cmdSave.Visible = False Then
  370.         cmdUpdate.Enabled = True
  371.     End If
  372. End Sub
  373. Sub txtLastName_KeyDown (KeyCode As Integer, Shift As Integer)
  374.     If KeyCode = KEY_DELETE And cmdSave.Visible = False Then
  375.         cmdUpdate.Enabled = True
  376.     End If
  377. End Sub
  378. Sub txtLastName_KeyPress (KeyAscii As Integer)
  379.     If cmdSave.Visible = False Then
  380.         cmdUpdate.Enabled = True
  381.     End If
  382. End Sub
  383.